home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / map / offscreen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-09  |  2.2 KB  |  77 lines  |  [TEXT/KAHL]

  1. #include <Events.h>
  2. #include <Quickdraw.h>
  3. #include <Windows.h>
  4. #include <Memory.h>
  5. #include "trigtab.h"
  6.  
  7. #define kIsVisible true
  8. #define kNoGoAway false
  9. #define kNoWindowStorage 0L
  10. #define kFrontWindow ((WindowPtr) -1L)
  11.  
  12. Boolean CreateOffscreenBitMap(GrafPtr *newOffscreen, Rect *inBounds)
  13. {
  14.     GrafPtr savePort;
  15.     GrafPtr newPort;
  16.  
  17.     GetPort(&savePort); /* need this to restore thePort after OpenPort */
  18.  
  19.     newPort = (GrafPtr)NewPtr(sizeof(GrafPort));/* allocate the grafPort */
  20.     if(MemError() != noErr)
  21.         return false;                                    
  22. /* failure to allocate off-screen port */
  23.  
  24.     /*    the call to OpenPort does the following:
  25.  
  26.         allocates space for visRgn (set to screenBits.bounds)
  27.         and clipRgn (set wide open)
  28.         sets portBits to screenBits
  29.         sets portRect to screenBits.bounds
  30.         etc. (see Inside Macintosh Volume 1 pages 163-164)
  31.         side effect: does a SetPort (&offScreen)
  32.     */
  33.  
  34.     OpenPort(newPort);
  35.  
  36.     /* make bitmap the size of the bounds that caller supplied */
  37.     newPort->portRect = *inBounds;
  38.     newPort->portBits.bounds = *inBounds;
  39.     RectRgn(newPort->clipRgn, inBounds);
  40.     RectRgn(newPort->visRgn, inBounds);
  41. /* rowBytes is size of row, must be rounded up to an even number of bytes */
  42.  
  43.     newPort->portBits.rowBytes =
  44.         ((inBounds->right - inBounds->left + 15) >> 4) << 1;
  45.  
  46.     /* number of bytes in BitMap is rowBytes * number of rows */
  47.     /* see notes at end of example about using NewPtr instead of NewHandle */
  48.     newPort->portBits.baseAddr =
  49.         NewPtr(newPort->portBits.rowBytes * (long)(inBounds->bottom - inBounds->top));
  50.  
  51.     if(MemError() != noErr)
  52.         {
  53.         SetPort(savePort);
  54.         ClosePort(newPort);        /* dump the visRgn and clipRgn */
  55.         DisposPtr((Ptr)newPort);    /* dump the GrafPort */
  56.         return false;    /* tell caller we failed */
  57.         }
  58.  
  59.     /* since the bits are just memory, let's clear them before we start */
  60.     EraseRect(inBounds);/* OpenPort did a SetPort(newPort) so we are OK*/
  61.     *newOffscreen = newPort;
  62.     SetPort(savePort);
  63.     return true;                    /* success */
  64. }
  65.  
  66. /*
  67.     DestroyOffscreenBitMap - get rid of an off-screen bitmap created
  68.     by CreateOffscreenBitMap
  69. */
  70. void DestroyOffscreenBitMap(GrafPtr oldOffscreen)
  71. {
  72.     ClosePort(oldOffscreen);        /* dump the visRgn and clipRgn */
  73.     DisposPtr(oldOffscreen->portBits.baseAddr);    /* dump the bits */
  74.     DisposPtr((Ptr)oldOffscreen);        /* dump the port */
  75. }
  76.  
  77.